import { useBaseApi } from '/@/api/base';

// 小智智能客服接口服务
export const useCustomerServiceApi = () => {
  const baseApi = useBaseApi("customerService");
  return {
    // 发送消息给小智智能客服
    sendMessage: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'sendMessage',
      method: 'post',
      data
    }),
    // 获取对话历史
    getChatHistory: (conversationId: string) => baseApi.request({
      url: baseApi.baseUrl + 'getChatHistory',
      method: 'get',
      params: { conversationId }
    }),
    // 创建工单
    createWorkOrder: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'createWorkOrder',
      method: 'post',
      data
    }),
    // 转接人工客服
    transferToHuman: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'transferToHuman',
      method: 'post',
      data
    }),
    // 获取工单状态
    getWorkOrderStatus: (workOrderId: number) => baseApi.request({
      url: baseApi.baseUrl + 'getWorkOrderStatus',
      method: 'get',
      params: { workOrderId }
    }),
    // 更新工单
    updateWorkOrder: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'updateWorkOrder',
      method: 'post',
      data
    }),
    // 获取个人记忆列表（分页）
    getPersonalMemoryPage: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'GetPersonalMemoryPage',
      method: 'post',
      data
    }),
    // 删除个人记忆
    deletePersonalMemory: (data: any) => baseApi.request({
      url: baseApi.baseUrl + 'DeletePersonalMemory',
      method: 'post',
      data
    }),
    // 批量删除个人记忆
    batchDeletePersonalMemory: (data: number[]) => baseApi.request({
      url: baseApi.baseUrl + 'BatchDeletePersonalMemory',
      method: 'post',
      data
    }),
    // 获取对话历史列表（分页）
    getConversationHistoryPage: (data: ConversationHistoryPageInput) => baseApi.request({
      url: baseApi.baseUrl + 'GetConversationHistoryPage',
      method: 'post',
      data
    }),
    // 删除对话历史
    deleteConversationHistory: (data: { id: number }) => baseApi.request({
      url: baseApi.baseUrl + 'DeleteConversationHistory',
      method: 'post',
      data
    }),
    // 批量删除对话历史
    batchDeleteConversationHistory: (data: number[]) => baseApi.request({
      url: baseApi.baseUrl + 'BatchDeleteConversationHistory',
      method: 'post',
      data
    }),
    // 清空所有个人记忆
    clearAllPersonalMemory: () => baseApi.request({
      url: baseApi.baseUrl + 'ClearAllPersonalMemory',
      method: 'post'
    }),
    // 清空所有对话历史
    clearAllConversationHistory: () => baseApi.request({
      url: baseApi.baseUrl + 'ClearAllConversationHistory',
      method: 'post'
    }),
  }
}

// 个人记忆实体
export interface PersonalMemoryOutput {
  id: number;
  userId: number;
  conversationId: string;
  role: 'user' | 'assistant';
  content: string;
  createdAt: string;
}

// 个人记忆分页结果
export interface PersonalMemoryPageResult {
  total: number;
  items: PersonalMemoryOutput[];
}

// 个人记忆查询输入
export interface PersonalMemoryPageInput {
  userId?: number;
  keyword?: string;
  page: number;
  pageSize: number;
}

// 对话历史查询输入
export interface ConversationHistoryPageInput {
  userId?: number;
  conversationId?: string;
  keyword?: string;
  page: number;
  pageSize: number;
}

// 对话历史输出
export interface ConversationHistoryOutput {
  id: number;
  userId: number;
  userName: string;
  message: string;
  messageType: string;
  timestamp: string;
  isFromUser: boolean;
  conversationId: string;
  actions?: string;
}

// 对话历史分页结果
export interface ConversationHistoryPageResult {
  total: number;
  items: ConversationHistoryOutput[];
}

// 智能客服消息实体
export interface CustomerServiceMessage {
  id?: number;
  userId: number;
  userName: string;
  message: string;
  messageType: 'text' | 'image' | 'file';
  timestamp: string;
  isFromUser: boolean;
  conversationId: string;
}

// 工单实体
export interface WorkOrder {
  id?: number;
  orderCode: string;
  userId: number;
  userName: string;
  title: string;
  description: string;
  priority: 'low' | 'medium' | 'high' | 'urgent';
  status: 'pending' | 'processing' | 'resolved' | 'closed';
  category: 'inventory' | 'order' | 'warehouse' | 'system' | 'other';
  assignedTo?: number;
  assignedToName?: string;
  createTime: string;
  updateTime?: string;
  conversationHistory: CustomerServiceMessage[];
  relatedOrderId?: number;
  relatedOrderCode?: string;
}

// 智能客服响应
export interface CustomerServiceResponse {
  message: string;
  messageType: 'text' | 'action' | 'transfer';
  actions?: {
    type: 'create_order' | 'check_inventory' | 'transfer_human' | 'create_work_order';
    data?: any;
  }[];
  workOrderCreated?: boolean;
  workOrderId?: number;
  transferToHuman?: boolean;
  conversationId: string;
}